'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide         = require('page/common/nav-side/index.js');
var _mm             = require('util/mm.js');
var _user           = require('service/user-service.js');
var templateIndexUser   = require('./indexUser.string');
var templateIndexTotur  = require('./indexTutor.string');

// page 逻辑部分
var page = {
    init: function(){
        this.onLoad();
    },
    onLoad : function(){
        // 初始化左侧菜单
        navSide.init({
            name: 'user-center'
        });
        // 加载用户信息
        this.loadUserInfo();
		this.bindEvent();
    },
	bindEvent : function(){
	    var _this = this;
	    // 点击提交按钮后的动作
	    $(document).on('click', '.btn-submit', function(){
	        var userInfo = {
	            stateId       : $.trim($('#stateId').val())
	        }
				if(userInfo.stateId === '请选择试题的阶段'){
					alert('请选择试题的阶段后再点击生成试卷');
				}else{
					window.location.href = './paper.html?stateId='+userInfo.stateId;
				}
	    });
	},
    // 加载用户信息
    loadUserInfo : function(){
        var ToturHtml  = '';
		var UserHtml   = '';
        _user.getUserInfo(function(res){
			//alert(res.user.role);
			if(res.user.role == 2){
				ToturHtml = _mm.renderHtml(templateIndexTotur, res);
				$('.panel-body').html(ToturHtml);
			}else{
				UserHtml = _mm.renderHtml(templateIndexUser, {
				    state :  res.state,
					user  :  res.user
				});
				$('.panel-body').html(UserHtml);
			}			
        }, function(errMsg){
            _mm.errorTips(errMsg);
        });
    }
};
$(function(){
    page.init();
});